package com.cheyunkeji.er.view.auction;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.text.format.DateUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.utils.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/** Created by Administrator on 2017/2/14. */
public class NotifyItemView extends RelativeLayout implements View.OnClickListener {

  @BindView(R.id.iv_left_img)
  ImageView ivLeftImg;
  //    @BindView(R.id.tv_notify_number)
  //    TextView tvNotifyNumber;
  @BindView(R.id.fl_left_img)
  RelativeLayout flLeftImg;

  @BindView(R.id.iv_arrow_guide)
  ImageView ivArrowGuide;

  @BindView(R.id.tv_title)
  TextView tvTitle;

  @BindView(R.id.tv_desc)
  TextView tvDesc;

  @BindView(R.id.tv_last_time)
  TextView tvLastTime;

  private View rootView;
  private Context context;
  private Drawable normalDrawable, pressedDrawable;
  private Badge unreadMsgCountView;

  public NotifyItemView(Context context) {
    super(context);
    initView(context);
  }

  public NotifyItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NotifyItemView);

    for (int i = 0; i < typedArray.getIndexCount(); i++) {

      switch (typedArray.getIndex(i)) {
        case R.styleable.NotifyItemView_img_normal:
          normalDrawable = typedArray.getDrawable(typedArray.getIndex(i));
          //                    ivLeftImg.setBackground(drawable);
          break;
        case R.styleable.NotifyItemView_img_pressed:
          pressedDrawable = typedArray.getDrawable(typedArray.getIndex(i));
          break;
        case R.styleable.NotifyItemView_txt_item_title:
          tvTitle.setText(typedArray.getString(typedArray.getIndex(i)));
          break;
        case R.styleable.NotifyItemView_txt_item_desc:
          tvDesc.setText(typedArray.getString(typedArray.getIndex(i)));
          break;
        default:
          break;
      }
    }
    /*
            String txtTitle = typedArray.getString(R.styleable.NotifyItemView_txt_item_title);
            String txtDesc = typedArray.getString(R.styleable.NotifyItemView_txt_item_desc);

            int imgNormal = typedArray.getInteger(R.styleable.NotifyItemView_img_normal,-1);
            int imgPressed = typedArray.getInteger(R.styleable.NotifyItemView_img_pressed,-1);


            Log.e(NotifyItemView.class.getSimpleName(), "NotifyItemView: txtTitle "+txtTitle);
            Log.e(NotifyItemView.class.getSimpleName(), "NotifyItemView: txtDesc "+txtDesc);
            Log.e(NotifyItemView.class.getSimpleName(), "NotifyItemView: imgNormal "+imgNormal);
            Log.e(NotifyItemView.class.getSimpleName(), "NotifyItemView: imgPressed "+imgPressed);
    */
    typedArray.recycle();
    //        ivLeftImg.setBackgroundDrawable(addStateDrawable(context, normalDrawable,
    // pressedDrawable, pressedDrawable));
    ivLeftImg.setImageDrawable(
        addStateDrawable(context, normalDrawable, pressedDrawable, pressedDrawable));
    //        tvTitle.setText(txtTitle);

  }

  public NotifyItemView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
  }

  private void initView(Context context) {
    rootView = LayoutInflater.from(context).inflate(R.layout.er_notify_item_layout, this, true);
    ButterKnife.bind(this, rootView);

    unreadMsgCountView =
        new QBadgeView(context)
            .bindTarget(flLeftImg)
            .setBadgeGravity(Gravity.TOP | Gravity.END)
            .setShowShadow(false);
  }

  private StateListDrawable addStateDrawable(
      Context context, int idNormal, int idPressed, int idFocused) {
    StateListDrawable sd = new StateListDrawable();
    Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
    Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
    Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
    // 注意该处的顺序，只要有一个状态与之相配，背景就会被换掉
    // 所以不要把大范围放在前面了，如果sd.addState(new[]{},normal)放在第一个的话，就没有什么效果了
    sd.addState(new int[] {android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
    sd.addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
    sd.addState(new int[] {android.R.attr.state_focused}, focus);
    sd.addState(new int[] {android.R.attr.state_pressed}, pressed);
    sd.addState(new int[] {android.R.attr.state_enabled}, normal);
    sd.addState(new int[] {}, normal);
    return sd;
  }

  /**
   * 通过代码创建背景选择器
   *
   * @param context 上下文
   * @param normalDrawable normal状态下的Drawable对象
   * @param pressedDrawable pressed状态下的Drawable对象
   * @param focusedDrawable focused状态下的Drawable对象
   * @return
   */
  private StateListDrawable addStateDrawable(
      Context context,
      Drawable normalDrawable,
      Drawable pressedDrawable,
      Drawable focusedDrawable) {
    StateListDrawable sd = new StateListDrawable();
    //        Drawable normal = idNormal == -1 ? null :
    // context.getResources().getDrawable(idNormal);
    //        Drawable pressed = idPressed == -1 ? null :
    // context.getResources().getDrawable(idPressed);
    //        Drawable focus = idFocused == -1 ? null :
    // context.getResources().getDrawable(idFocused);
    // 注意该处的顺序，只要有一个状态与之相配，背景就会被换掉
    // 所以不要把大范围放在前面了，如果sd.addState(new[]{},normal)放在第一个的话，就没有什么效果了
    sd.addState(
        new int[] {android.R.attr.state_enabled, android.R.attr.state_focused}, focusedDrawable);
    sd.addState(
        new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled}, pressedDrawable);
    sd.addState(new int[] {android.R.attr.state_focused}, focusedDrawable);
    sd.addState(new int[] {android.R.attr.state_pressed}, pressedDrawable);
    sd.addState(new int[] {android.R.attr.state_enabled}, normalDrawable);
    sd.addState(new int[] {}, normalDrawable);
    return sd;
  }

  @Override
  public void onClick(View v) {
    Log.e("=====", "==========");
  }

  public void setUnreadMsgCount(int msgCount) {

    unreadMsgCountView.setBadgeNumber(msgCount);
  }

  public void showContent(String title, String contentBody, String time) {

    tvDesc.setText(contentBody);

    //      时间在今天之内，则显示时分秒，否则显示年月日
    if (DateUtils.isToday(Long.valueOf(time) * 1000)) {
      tvLastTime.setText(TimeUtils.millis2StringWithoutYMD(Long.valueOf(time) * 1000));
    } else {
      tvLastTime.setText(TimeUtils.millis2StringWithoutHMS(Long.valueOf(time) * 1000));
    }
  }
}
